/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package swagger

import (
	"net/http"
	"gitee.com/tugoer/orivil/mux"
	"gitee.com/tugoer/orivil/export"
	"gitee.com/tugoer/orivil/log"
)

const (
	swaggerDist = "public/swagger"
)

var apiServer = http.StripPrefix("/api", http.FileServer(http.Dir(swaggerDist)))

func init() {
	export.ExportFiles(".", "export", log.Info)
}

var Actions = []*mux.Action {
	{
		Name:   "Swagger Api Service",
		Method: http.MethodGet,
		Route:  "api/",
		Handler: func(ctx *mux.Context) {
			// 跳转至目录, 不然无法定位样式文件
			if ctx.URL.Path == "/api" {
				ctx.Redirect("/api/")
			}
			ctx.AllowCrossSiteAccess()
			apiServer.ServeHTTP(ctx.Response, ctx.Request)
		},
	},

	{
		Name:   "Swagger Api Config Service",
		Method: http.MethodGet,
		Route:  "api-data",
		Handler: func(ctx *mux.Context) {
			ctx.AllowCrossSiteAccess()
			ctx.SendJson(mux.Handler.API2)
		},
	},
}